package com.yyg.boot.web;

import com.yyg.boot.entity.User;
import com.yyg.boot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @Author 一一哥Sun
 * @Date Created in 2020/4/29
 * @Description Description
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * save user
     */
    @PostMapping(value = "/save")
    public User save(@RequestBody User user) {
        return userService.saveUser(user);
    }

    /**
     * find user by username
     */
    @GetMapping(value = "/find/{username}")
    public User findUser(@PathVariable String username) {
        return userService.findByUsername(username);
    }

    /**
     * user login
     */
    @GetMapping(value = "/login")
    public User login(String username, String password, HttpSession session) {
        User user = userService.login(username, password);
        if(user!=null){
            session.setAttribute(session.getId(),user);
        }
        return user;
    }

    @GetMapping(value = "/logout")
    public String logout(HttpSession session){
        session.removeAttribute(session.getId());
        return "user logout success";
    }

}
